package com;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import com.Constants;
import com.admin.User;



public class LoginFilter extends BaseAction implements Filter{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public  String Logcode;
	

	

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void doFilter(ServletRequest sRequest, ServletResponse sResponse,
			FilterChain filterChain) throws IOException, ServletException {
       
		HttpServletRequest request = (HttpServletRequest) sRequest;
		HttpServletResponse response = (HttpServletResponse) sResponse;
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		String url = request.getServletPath();
		String contextPath = request.getContextPath();
		Object user = session.getAttribute(Constants.USER);
		if (url.equals(""))
			url += "/";
		if (!"/".equals(url) && url.indexOf("/login") < 0
				&& url.indexOf("/css") < 0 
				&& url.indexOf("/js") < 0
				&& url.indexOf("/dologin") < 0
				&& url.indexOf("/images") < 0
				&& url.indexOf("/fonts") < 0
				&& url.indexOf("/savesqxx") < 0
				&& url.indexOf("/xzfj") < 0
				
				)
				{
			
			if (user == null) {
				response.sendRedirect(contextPath + "/login");
				return;
			}
			
		}
		
		
		filterChain.doFilter(sRequest, sResponse);
	}
	
	

	
	







	@SuppressWarnings({ "rawtypes", "unchecked" })
	private List<LinkedHashMap<String, String>>  selecturl(String url) {
	Map map=new HashMap();
	map.put("table", "\"Logdictionary\"");
	map.put("where", "\"Url\" = '"+url+"'");
	map.put("px", "\"Logid\"");
	map.put("nr", "*");
	List<LinkedHashMap<String, String>> list=selecttable(map);
	return list;	
	}
	
	
	
	
	





	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  void savelog(Map pharm) {
		Map map=new HashMap();
		map.put("table", "\"Loginformation\"");
		String zdm="";
		String nr="";
		Iterator<String> iter = pharm.keySet().iterator();
		while(iter.hasNext()){
			   String key=iter.next();
			   String value = pharm.get(key).toString();
			   if(zdm.equals("")) {
				   
				   zdm="\""+key+"\"";
				   nr="'"+value+"'";
				}else {
					
					zdm=zdm+",\""+key+"\"";
					nr=nr+",'"+value+"'";
				}
		}
		map.put("zdm", zdm);
		map.put("nr", nr);
		inserttable(map);
	}








	public static String getCliectIp(HttpServletRequest request){
        String ip = request.getHeader("X-Real-IP");
        if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
        ip = request.getHeader("X-Forwarded-For");
        if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
            // 澶氭鍙嶅悜浠ｇ悊鍚庝細鏈夊涓狪P鍊硷紝绗竴涓负鐪熷疄IP銆�
            int index = ip.indexOf(',');
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        } else {
            return request.getRemoteAddr();
        }
    }

	
	
	public void init(FilterConfig config) throws ServletException {

	}












	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
}